package il.ac.technion.cs236700.address_book;

import static org.junit.Assert.*;

import il.ac.technion.cs236700.address_book.Address;
import il.ac.technion.cs236700.address_book.AddressBook;
import il.ac.technion.cs236700.address_book.AddressBookImpl;
import il.ac.technion.cs236700.address_book.Defaults;
import il.ac.technion.cs236700.address_book.Person;
import il.ac.technion.cs236700.address_book.Samples;
import il.ac.technion.cs236700.utils.Misc;
import il.ac.technion.cs236700.utils.STRING;
import il.ac.technion.cs236700.utils.CSV.Separator;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class AddressBookImpl_Test{
	

	   protected static String[] arr(String... elements) {
	      return elements;
	   }
	   
	   /**
	    * A factory method 
	    * @return A new address book object
	    */
	
	protected AddressBookImpl newAddressBook() {
		AddressBookImpl ab = new AddressBookImpl();
		ab.add(Misc.map(Defaults.FirstName, "Wall-E").map(Defaults.LastName,"Mall-E")
		         .map(Defaults.HomePage,"http://www.imdb.com/title/tt0910970").map);
		ab.add(Misc.map(Defaults.FirstName, "Wall-E")
		         .map(Defaults.LastName, "Mall-E").map);
		ab.add(Misc.map(Defaults.FirstName, "Adam").map(Defaults.LastName,"Adam")
		         .map(Defaults.HomePage, "adam.homepage.com").map);
		ab.add(Misc.map(Defaults.FirstName, "Eve").map(Defaults.LastName,"Eve")
		         .map("Home Address", "Paradise").map);
		ab.add(Misc.map(Defaults.FirstName, "Ebay").map(Defaults.LastName,"dotcom")
		         .map(Defaults.HomePage, "http://www.ebay.com").map);
		ab.add(Misc.map(Defaults.FirstName, "Eve").map(Defaults.LastName,"Eve").map				
				(Defaults.Email,"sales@ebay.com").map);
		return ab;
	}
	@Before
	public void setUp() throws Exception {
		
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testAddPersonAddressAddress() {
		AddressBookImpl ab = newAddressBook();
	    Person p = new Person("Rick", "Deckard", "");
	    Address add = new Address("", "LAPD", "LA", "", "");  
		ab.add(p,null,add);
	    assertEquals(true, ab.exists(p));
	}

	@Test
	public void testGetConflicts() {
		AddressBookImpl ab = newAddressBook();
		ab.add(Misc.map(Defaults.FirstName, "David")
         .map(Defaults.LastName, "Gal").map(Defaults.Email,"dave@walla").map);
		ab.add(Misc.map(Defaults.FirstName, "David")
		 .map(Defaults.LastName, "Gal").map(Defaults.Email,"dave@nana").map);
		ab.setOutput(Defaults.FirstName);
		String $ = Separator.separateBy(ab.getOutputFields(), ",")+"\n";
		$ += "David"+"\n" ;
		$ += "David"+"\n";
		assertEquals($,ab.getConflicts());
	}

	@Test
	public void testGetNormalized() {
		AddressBook ab = new AddressBookImpl();
		ab.setOutput(Samples.FIELDS_1);
		ab.load(Samples.CSV1);
		Assert.assertEquals(Samples.CSV2, ab.getNormalized());
		ab = new AddressBookImpl();
		ab.setOutput(Samples.FIELDS_11);
		ab.load(Samples.CSV11);
		Assert.assertEquals(Samples.CSV111, ab.getNormalized());
	}

	@Test
	public void testGetSupportedKeys() {
		AddressBook ab = new AddressBookImpl();
		String $1 = STRING.cat(Defaults.SUPPORTED_KEYS.toArray(new String[Defaults.SUPPORTED_KEYS.size()]));
		String $2 = STRING.cat(ab.getSupportedKeys());
		Assert.assertEquals($1,$2);
	}

	@Test
	public void testLoad() {
		AddressBook ab = new AddressBookImpl();
	      ab.setOutput(Samples.FIELDS_1);
	      ab.load(Samples.CSV2);
	      Assert.assertEquals(Samples.CSV2, ab.getNormalized());
		
	}
}
